package com.insigma.business.qrcode.intell._bff.impl;

import cn.hutool.core.bean.BeanUtil;
import com.google.common.collect.Lists;
import com.insigma.business.qrcode.common.dto.CommonQaInfoSearchRequest;
import com.insigma.business.qrcode.common.dto.QaResult;
import com.insigma.business.qrcode.common._consumer.CommonQaInfoConsumer;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.template.area.ServiceTemplate;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.RpcContextUtil;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.common.vo.CommentResponse;
import com.insigma.business.qrcode.common.vo.ProcessStatusEnum;
import com.insigma.business.qrcode.common.vo.SessionSoldier;
import com.insigma.business.qrcode.intell._bff.CareSoldierDemandBffService;
import com.insigma.business.qrcode.intell._consumer.CareSoldierDemandConsumer;
import com.insigma.business.qrcode.intell.dto.CareSoldierDemandInsertRequest;
import com.insigma.business.qrcode.soldier.dto.CareSoldierDemandDTO;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.soldier.request.CareSoldierDemandSearchRequest;
import com.insigma.business.qrcode.soldier.request.CommonQaInfoUpdateRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class CareSoldierDemandBffServiceImpl implements CareSoldierDemandBffService {

    @Autowired
    private CommonQaInfoConsumer commonQaInfoConsumer;

    @Resource
    CareSoldierDemandConsumer careSoldierDemandConsumer;

    @Autowired
    protected ServiceTemplate serviceTemplate;

    @Override
    public BaseResponse<QaResult> intelligentQA(CommonQaInfoSearchRequest searchRequest) {
        BaseResponse<QaResult> qaResultBaseResponse = commonQaInfoConsumer.intelligentQA(searchRequest);
        return qaResultBaseResponse;

    }

    @Override
    public BaseResponse<String> saveCareSoldierDemandBySoldier(CareSoldierDemandInsertRequest request) {
        SessionSoldier sessionSoldier = RpcContextUtil.getSessionSoldier();
        request.setSoldierId(sessionSoldier.getUserId());
        return careSoldierDemandConsumer.saveCareSoldierDemandToHz(request);
    }

    @Override
    public BasePagingResponse<List<CareSoldierDemandDTO>> searchMyCareSoldierDemand(CareSoldierDemandSearchRequest request) {
        //获取士兵ID部分暂时注释
        if (false) {
            SessionSoldier sessionSoldier = RpcContextUtil.getSessionSoldier();
            request.setSoldierId(sessionSoldier.getUserId());
        }
        BasePagingResponse<List<CareSoldierDemandDTO>> listBasePagingResponse = careSoldierDemandConsumer.searchCareSoldierDemand(request);
        return listBasePagingResponse;
    }

    @Override
    public BaseResponse<CareSoldierDemandDTO> getCareSoldierDemand(final BaseGetRequest baseGetRequest){

        final BaseResponse<CareSoldierDemandDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条CareSoldierDemand的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final BaseResponse<CareSoldierDemandDTO> careSoldierDemandResponse = careSoldierDemandConsumer.getCareSoldierDemand(baseGetRequest);
                CareSoldierDemandDTO dto = careSoldierDemandResponse.getDto();
                if (dto != null) {
                    dto.setComments(buildComment(dto));
                }

                BeanUtil.copyProperties(careSoldierDemandResponse, baseResponse);

            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_SOLDIER_DEMAND_GET;
            }
        });
        return baseResponse;
    }

    private List<CommentResponse> buildComment(CareSoldierDemandDTO dto) {
        CommentResponse c1 = new CommentResponse();
        c1.setTime(dto.getCreateDatetime());
        c1.setMessage("创建咨询");

        List<CommentResponse> list = Lists.newArrayList(c1);

        if (ProcessStatusEnum.SUCCESS.name().equals(dto.getStatus()) || ProcessStatusEnum.REJECT.name().equals(dto.getStatus())) {
            CommentResponse cs = new CommentResponse();
            cs.setTime(dto.getModifyDatetime());
            cs.setMessage("结果："+dto.getFeedback());
            list.add(cs);
        }

        if (dto.getHappenDatetime() != null) {
            CommentResponse cs = new CommentResponse();
            cs.setTime(dto.getHappenDatetime());
            cs.setMessage("评价：" + dto.getEvaluationStar() + "星," + dto.getEvaluationContent());
            list.add(cs);
        }

        return list;
    }


    @Override
    public BaseResponse<String> evaluateQa(CommonQaInfoUpdateRequest request) {

        return commonQaInfoConsumer.evaluateQa(request);
    }

    @Override
    public BaseResponse<String> evaluateDemand(CareSoldierDemandInsertRequest careSoldierDemandInsertRequest) {

        SessionSoldier sessionSoldier = RpcContextUtil.getSessionSoldier();
        BizCheckUtil.checkNotNull(sessionSoldier, "用户非法");
        careSoldierDemandInsertRequest.setCreatorId(sessionSoldier.getUserId());

        return careSoldierDemandConsumer.evaluateDemand(careSoldierDemandInsertRequest);
    }
}
